package me.nujoug.foctapp;

import me.nujoug.net.dao.Department;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Adapter to bind a ToDoItem List to a view
 */
public class DepItemAdapter extends ArrayAdapter<Department> {

	/**
	 * Adapter context
	 */
	Context mContext;

	/**
	 * Adapter View layout
	 */
	int mLayoutResourceId;

	public DepItemAdapter(Context context, int layoutResourceId) {
		super(context, layoutResourceId);

		mContext = context;
		mLayoutResourceId = layoutResourceId;
	}

	/**
	 * Returns the view for a specific item on the list
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;

		final Department currentItem = getItem(position);

		if (row == null) {
			LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
			row = inflater.inflate(mLayoutResourceId, parent, false);
		}

		row.setTag(currentItem);
		final TextView itemView = (TextView) row.findViewById(R.id.DepItem);
		itemView.setText(currentItem.getBigDepName() + " - " + currentItem.getName());

		itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
		        Bundle bundle = new Bundle();
		        bundle.putString("hospID", currentItem.getHospID());
		        bundle.putString("hospName", currentItem.getHospName());
		        bundle.putString("depID", currentItem.getID());
		        bundle.putString("depName", currentItem.getName());
		        ((FoctWebBaseActivity) mContext).passWebData(bundle);
		        Intent intent = new Intent(mContext, DateListActivity.class);
		        intent.putExtras(bundle);
		        mContext.startActivity(intent);
			}
		});

		return row;
	}

}
